/**
 * XQUtilities.java
 * Nov 19, 2013
 * Copyright: phongtdse60732@fpt.edu.vn
 */
package XMLUtilities;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

import javax.xml.namespace.QName;
import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQExpression;
import javax.xml.xquery.XQResultSequence;

import net.sf.saxon.xqj.SaxonXQDataSource;

/**
 * @author trandophong
 * @version 1.0
 */
public class XQUtilities {

	public static String getMenuXML(String realPath, String strMenuId) {

		XQConnection conn = null;
		XQExpression exp = null;
		XQResultSequence result = null;
		InputStream query = null;
		SaxonXQDataSource ds = new SaxonXQDataSource();

		realPath = realPath.replace("\\", "/");
		String menuXML = realPath + "WEB-INF/xml/menu.xml";
		String planXML = realPath + "WEB-INF/xml/plan.xml";
		String planXQ = realPath + "WEB-INF/xquery/plan.xq";

		try {
			conn = ds.getConnection();

			query = new FileInputStream(planXQ);

			exp = conn.createExpression();

			exp.bindString(new QName("menuXMLDocPath"), menuXML, null);
			exp.bindString(new QName("planXMLDocPath"), planXML, null);
			exp.bindString(new QName("menuId"), strMenuId, null);

			result = exp.executeQuery(query);

			Properties pros = new Properties();
			pros.setProperty("method", "xml");
			pros.setProperty("indent", "yes");

			return result.getSequenceAsString(pros);

		} catch (XQException e) {
		} catch (FileNotFoundException e) {
		}

		return null;
	}

	public static String getRecipeIncludeDish(String realPath, int recipeId) {
		XQConnection connection = null;
		XQExpression expressionRecipe = null;
		XQExpression expressionRecipeDetail = null;
		XQResultSequence resultSequence = null;
		InputStream queryRecipe = null;
		InputStream queryRecipeDetail = null;
		SaxonXQDataSource ds = new SaxonXQDataSource();
		String result = "";

		realPath = realPath.replace("\\", "/");
		String recipeXML = realPath + "WEB-INF/xml/recipe.xml";
		String recipeDetailXML = realPath + "WEB-INF/xml/recipe-detail.xml";
		String recipeXQ = realPath + "WEB-INF/xquery/recipe.xq";
		String recipeDetailXQ = realPath + "WEB-INF/xquery/recipe-detail.xq";

		try {
			connection = ds.getConnection();
			queryRecipe = new FileInputStream(recipeXQ);
			queryRecipeDetail = new FileInputStream(recipeDetailXQ);
			expressionRecipe = connection.createExpression();
			expressionRecipeDetail = connection.createExpression();

			Properties pros = new Properties();
			pros.setProperty("method", "xml");
			pros.setProperty("indent", "yes");
			
			expressionRecipe.bindString(new QName("recipeXMLDocPath"), recipeXML, null);
			expressionRecipe.bindString(new QName("recipeId"), String.valueOf(recipeId), null);
			resultSequence = expressionRecipe.executeQuery(queryRecipe);

			result += resultSequence.getSequenceAsString(pros);
			result = result.substring(0, result.length() - 9);
			
			expressionRecipeDetail.bindString(new QName("recipeDetailXMLDocPath"), recipeDetailXML, null);
			expressionRecipeDetail.bindString(new QName("recipeId"), String.valueOf(recipeId), null);
			resultSequence = expressionRecipeDetail.executeQuery(queryRecipeDetail);

			result += "<dishList>" + resultSequence.getSequenceAsString(pros)
					+ "</dishList></Recipe>";
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
}
